<?php

class Model_Gateway
{
  
	protected $login_url = '';
	protected $session_id = '';
	protected $cookie_file = '';
  
	public function __construct($login_url)
	{
	   $this->login_url = $login_url;
		
		 $this->cookie_file = '/tmp/yesup_account_cookies.txt';
	   
	   if($this->session_id=='')
      $this->session_id = $this->getSession($login_url);
	}
	
  private function getSession($login_url)
  {

    $headers = get_headers($login_url);
    for($i=0;$i<count($headers);$i++)
    {
      $m = array();
      preg_match_all('/PHPSESSID=([^;]+);/i', $headers[$i], $m);
      
      if(!empty($m[1][0])){
        return $m[1][0];
      }
    }
  }
	
	public function getSessionId()
  {
    if($this->session_id=='')
      $this->session_id = $this->getSession();
    return $this->session_id;
  }
	
	public function doServiceLogin($login_data)
  {
    //Zend_Debug::dump($this->login_url);
  	//Zend_Debug::dump($login_data);    
    //Zend_Debug::dump($this->session_id);    
    
    $curl = new App_Curl($this->session_id, true, $this->cookie_file);

    $post_data = http_build_query($login_data);
    $response_result = $curl->post($this->login_url,$post_data);
    //Zend_Debug::dump($response_result);
    
  }
}